/*
 * SplashFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.splash;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.data.response.SplashResponse;
import com.yidiandao.business.ui.guide.GuideActivity;
import com.yidiandao.business.ui.location.LocationActivity;
import com.yidiandao.business.ui.loginabout.login.LoginActivity;
import com.yidiandao.business.ui.main.MainActivity;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.JumpUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * splash界面
 */
public class SplashFragment extends BaseFragment implements SplashContract.View {

    private SplashContract.Presenter mPresenter;
    private ImageView mIvSplash;
    private TextView mTvTimeTip;
    private RelativeLayout mRlAd;
    private SplashResponse.DataBean.PictureInfoBean mBean;

    public static SplashFragment getInstance() {
        return new SplashFragment();
    }

    @Override
    public void setPresenter(SplashContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return SplashFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
    }

    @Override
    protected void initViews() {
        mRlAd = (RelativeLayout) mRootView.findViewById(R.id.rl_ad);
        mIvSplash = (ImageView) mRootView.findViewById(R.id.iv_splash);
        mTvTimeTip = (TextView) mRootView.findViewById(R.id.tv_time_tip);
        initEvent();
        mPresenter.start();
        mPresenter.initDelayTask();
    }

    private void initEvent() {
        mTvTimeTip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.startActivity();
            }
        });
        mIvSplash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.startActivity();
                JumpUtils.clickSplashAd(mBean, mActivity);
            }
        });
    }

    @Override
    public void toMainActivity() {
        MainActivity.actionStart(mActivity);
    }

    @Override
    public void toLocationActivity() {
        LocationActivity.actionStart(mActivity, false);
    }

    @Override
    public void toGuideActivity() {
        GuideActivity.actionStart(mActivity);
    }

    @Override
    public void finishActivity() {
        mActivity.finish();
    }

    @Override
    public void toLoginActivity() {
        LoginActivity.actionStart(mActivity);
    }

    @Override
    public Activity getCurrentActivity() {
        return mActivity;
    }

    @Override
    public void setSplashData(SplashResponse.DataBean.PictureInfoBean bean) {
        mBean = bean;
        mRlAd.setVisibility(View.VISIBLE);
        //广告图
        ImageLoader.loadImageWithThumbnail(mActivity, mIvSplash, bean.getPicture());
        //跳过
        setTimeTip(5);
    }

    @Override
    public void setTimeTip(int timeSecond) {
        mTvTimeTip.setText(UIUtils.getString(R.string.splash_time_tip, timeSecond));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_splash;
    }

    @Override
    protected boolean cancelRequest() {
        return false;
    }

    @Override
    public void onResume() {
        mPresenter.onFragmentResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        mPresenter.onFragmentPause();
        super.onPause();
    }
}
